import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:smart_ai_dart/pages/profile_page/controller.dart';

class ProfilePage extends GetView<ProfileController> {
  const ProfilePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('profile'.tr),
        actions: [
          IconButton(
            icon: const Icon(
              Icons.edit,
              color: Colors.white,
            ),
            onPressed: controller.editProfile,
          ),
        ],
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            _buildUserHeader(),
            const SizedBox(height: 20),
            _buildProfileOptions(context),
            const SizedBox(height: 20),
            _buildSystemOptions(context),
          ],
        ),
      ),
    );
  }

  Widget _buildUserHeader() {
    return Obx(() => GestureDetector(
          onTap: controller.changeAvatar,
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
            child: Row(
              children: [
                CircleAvatar(
                  radius: 32,
                  backgroundImage: controller.avatarUrl.value.isNotEmpty
                      ? NetworkImage(controller.avatarUrl.value)
                      : const AssetImage('assets/images/default_avatar.png')
                          as ImageProvider,
                ),
                const SizedBox(width: 16),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        controller.nickname.value,
                        style: const TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      const SizedBox(height: 4),
                      Text(
                        controller.email.value,
                        style: TextStyle(
                          color: Colors.grey[600],
                          fontSize: 14,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ));
  }

  Widget _buildProfileOptions(BuildContext context) {
    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 16),
      child: Column(
        children: [
          ListTile(
            leading: const Icon(Icons.language),
            title: Text('switch_language'.tr),
            trailing: const Icon(Icons.chevron_right),
            onTap: controller.toggleLanguage,
          ),
          const Divider(),
          ListTile(
            leading: const Icon(Icons.edit),
            title: Text('edit_profile'.tr),
            trailing: const Icon(Icons.chevron_right),
            onTap: controller.editProfile,
          ),
        ],
      ),
    );
  }

  Widget _buildSystemOptions(BuildContext context) {
    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 16),
      child: Column(
        children: [
          Obx(() => SwitchListTile(
                title: controller.isDarkMode.value
                    ? Text('light_mode'.tr)
                    : Text('dark_mode'.tr),
                secondary: controller.isDarkMode.value
                    ? const Icon(Icons.light_mode)
                    : const Icon(Icons.dark_mode),
                value: controller.isDarkMode.value,
                onChanged: (_) => controller.toggleTheme(),
              )),
          const Divider(),
          ListTile(
            leading: const Icon(Icons.logout),
            title: Text('logout'.tr),
            onTap: controller.logout,
          ),
        ],
      ),
    );
  }
}
